//
// Created by YxMin on 2024/7/4.
//

#ifndef GAME_CONTAINER_H
#define GAME_CONTAINER_H
#include "Element.h"
#include "Button.h"
#include "Input.h"
#include "TextArea.h"
#include <vector>
#include <UI.h>
class Container:public Element {
public:
    std::vector<Element*> contents;
    Container(int uid,int x1,int y1,int x2,int y2,int radius = 0,int(*callback)(void*) = nullptr)
    :Element(uid,x1,y1,x2,y2,radius,callback){}
    //圆角为-1时默认跟随父元素，x2和y2为0时默认跟随父元素
    //此处的坐标是相对父元素的坐标
    void addButton(int x1,int y1,int x2 = 0,int y2 = 0,int radius = -1,int(*callback)(void*) = nullptr,int(*DoubleClick)(void*) = nullptr,char* text = nullptr,COLORREF fontColor = BLACK,bool multiLine = false) {
        auto* btn = new Button(uid,
                               x1+area.left,
                               y1+area.top,
                               x2 == 0 ? area.right : x2+area.left,
                               y2 == 0 ? area.bottom : y2+area.top,
                               radius == -1 ? this->radius : radius,
                               callback,text,fontColor,multiLine);
        if (DoubleClick)
            btn->setDoubleClickCallback(DoubleClick);
        contents.push_back(btn);
    }
    void addInput(char *id,int x1,int y1,int x2 = 0,int y2 = 0,int radius = -1,LPCTSTR placeholder = nullptr) {
        auto* in = new Input(id,uid,
                               x1+area.left,
                               y1+area.top,
                               x2 == 0 ? area.right : x2+area.left,
                               y2 == 0 ? area.bottom : y2+area.top,
                               radius == -1 ? this->radius : radius,
                               placeholder);
        contents.push_back(in);
    }
    void addTextArea(int x1,int y1,int x2 = 0,int y2 = 0,int radius = -1,char* text = nullptr,int height = 24,int width = 0,COLORREF fontColor = BLACK,bool multiLine = false) {
        auto* in = new TextArea(uid,
                             x1+area.left,
                             y1+area.top,
                             x2 == 0 ? area.right : x2+area.left,
                             y2 == 0 ? area.bottom : y2+area.top,
                             radius == -1 ? this->radius : radius,
                             text,height,width,fontColor,multiLine);
        contents.push_back(in);
    }
    void addHorizontalButton(int height, int radius = -1,int(*callback)(void*) = nullptr,int(*DoubleClick)(void*) = nullptr,char* text = nullptr,COLORREF fontColor = BLACK,bool multiLine = false) {
        int top = 0,bottom;
        if(!contents.empty()) {
            auto it = contents.begin();
            //此做法有bug，请在之前add元素的时候从上到下，否则会出现意料之外的结果
            do{
                bottom = (*it)->getArea().top - area.top;
                if (bottom - top >= height) {
                    addButton(0,top,0,bottom,radius,callback,DoubleClick,text,fontColor,multiLine);
                    return;
                }
                top = (*it)->getArea().bottom - area.top;
                it++;
            }while(it != contents.end());
        }
        bottom = top + height;
        addButton(0,top,
                  0,bottom <= area.bottom-area.top ? bottom : area.bottom-area.top,
                  radius,callback,DoubleClick,text,fontColor,multiLine); //overflow: hidden
    }
    void addHorizontalInput(char* id,int height, int radius = -1,LPCTSTR placeholder = nullptr) {
        int top = 0,bottom;
        if(!contents.empty()) {
            auto it = contents.begin();
            //printf("1\n");
            //此做法有bug，请在之前add元素的时候从上到下，否则会出现意料之外的结果
            do {
                bottom = (*it)->getArea().top - area.top;
                if (bottom - top >= height) {
                    addInput(id, 0, top, 0, bottom, radius, placeholder);
                    return;
                }
                top = (*it)->getArea().bottom - area.top;
                it++;
            } while (it != contents.end());
        }
        bottom = top + height;
        addInput(id,0,top,
                  0,bottom <= area.bottom-area.top ? bottom : area.bottom-area.top,
                  radius,placeholder); //overflow: hidden
    }
    void draw() override {
        if(radius > 0) {
            roundrect(area.left,area.top,area.right,area.bottom,radius,radius);
        } else {
            rectangle(area.left,area.top,area.right,area.bottom);
        }
        auto it = contents.begin();
        while(it != contents.end()) {
            (*it)->draw();
            it++;
        }
    }

};


#endif //GAME_CONTAINER_H
